// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: SpaceInvadersApp.cpp
// Desrciption	: CSpaceInvadersApp implementation file.
// Author(s)	: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "SolitaireStd.h"

// Library Includes

// This Include
#include "SolitaireApp.h"

// Local Includes
#include "Msvc\resource.h"
#include "SolitaireLogic.h"

// Global Variables
CSolitaireApp g_solitaireApp;

CGameLogic* CSolitaireApp::VCreateGame()
{
	m_pGame = new CSolitaireLogic();
	if(!m_pGame->VInit()) {
	// Game failed to initialize.
		return NULL;
	}
	return m_pGame;
}

HICON CSolitaireApp::VGetIcon()
{
	return LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_ICON1)); 
}

TCHAR* CSolitaireApp::VGetTitle()
{
	return L"Solitaire";
}

CPoint CSolitaireApp::VGetScreenSize()
{
	return CPoint(g_kiSOLITAIRE_SCREEN_WIDTH, g_kiSOLITAIRE_SCREEN_HEIGHT);
}

DWORD CSolitaireApp::VGetWindowFlags()
{
	return WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU |WS_VISIBLE;
}

LPTSTR CSolitaireApp::VGetMenuName()
{
	return MAKEINTRESOURCE(IDR_MENU1);
}

bool CSolitaireApp::VInitialize(HINSTANCE _hInstance, LPSTR _lpCmdLine, HWND _hWnd, int _iFrameRate)
{
	if(!CGameApp::VInitialize(_hInstance, _lpCmdLine, _hWnd, _iFrameRate)) {
		return false;
	}
	else {
		// Create and set the option dialog
		m_hDialogOption = CreateDialog(g_pApp->GetInstance(),
										MAKEINTRESOURCE(IDD_DIALOG_OPTION),
										g_pApp->GetHwnd(), 
										&CSolitaireApp::OnDialogOptionEvent);

		CheckRadioButton(m_hDialogOption, IDC_RADIO_CARDDRAW_1, IDC_RADIO_CARDDRAW_3, IDC_RADIO_CARDDRAW_1);
	}

	return true;
}

bool CSolitaireApp::VOnCommand(WORD _command)
{
	CSolitaireLogic* pGame = dynamic_cast<CSolitaireLogic*>(g_pApp->m_pGame);

	if(!pGame) {
		return false;
	}
	else {
		switch(_command) {
			case ID_GAME_NEWGAME: {
				int iResult = MessageBox(g_pApp->GetHwnd(), 
										 L"Are you sure you want to start a new game?", 
										 L"New Game", 
										 MB_YESNO);

				if(iResult == IDYES) {
				// The user picked yes.
					// Reset game.
					pGame->SetupGame();
				}
				else {
				// The user picked no.
					// Don't reset.
				}

				return true;
			}
			break;
			case ID_GAME_OPTIONS: {
				ShowWindow(m_hDialogOption, SW_SHOW);
				return true;
			}
			break;
			case ID_HELP_INSTRUCTIONS: {
				// Get the text from a file.
				std::wstring wsTextOut = LoadTextFromFile("Text/Instructions.txt");

				// Ouput text into message box.
				MessageBox(g_pApp->GetHwnd(), wsTextOut.c_str(), L"Instructions", MB_OK | MB_ICONINFORMATION);

				return true;
			}
			break;
			case ID_HELP_CREDITS: {
				// Get the text to display from a file.
				std::wstring wsTextOut = LoadTextFromFile("Text/Credits.txt");
			
				// Output text into message box.
				MessageBox(g_pApp->GetHwnd(),  wsTextOut.c_str(), L"Credits", MB_OK | MB_ICONINFORMATION);

				return true;
			}
			break;
			case ID_GAME_QUIT: {
				int iResult = MessageBox(g_pApp->GetHwnd(), 
										 L"Are you sure you want to quit?", 
										 L"Quit Game", 
										 MB_YESNO);
				if(iResult == IDYES) {
				// The user picked yes.
					// Quit.
					g_pApp->OnClose();
				}
				else {
				// The user picked no.
					// Don't quit.
				}

				return true;
			}
			break;
			default: break;
		}
	}

	return false;
}

//==================================
// Dialog Option Events
//==================================
BOOL CALLBACK CSolitaireApp::OnDialogOptionEvent(HWND _hDlg, UINT _msg, WPARAM _wparam, LPARAM _lparam)
{
	switch(_msg)
	{
		case WM_COMMAND:
		{
			CSolitaireLogic* pGame = dynamic_cast<CSolitaireLogic*>(g_pApp->m_pGame);

			if(pGame) {
				switch(LOWORD(_wparam))
				{
					case IDOK:
					{
						if(IsDlgButtonChecked(_hDlg, IDC_RADIO_CARDDRAW_1)) {
							pGame->SetCardDrawAmount(1);
						}
						else if(IsDlgButtonChecked(_hDlg, IDC_RADIO_CARDDRAW_3)) {
							pGame->SetCardDrawAmount(3);
						}
						else {
							pGame->SetCardDrawAmount(1);
						}

						std::cout << "DIALOG OPTION: OK PRESSED" << std::endl;
						EndDialog(_hDlg, 0); // Close the dialog

						return true;
					}
					break;
					case IDC_RADIO_CARDDRAW_1:
					{
						std::cout << "DIALOG OPTION: CARDDRAW 1 SELECTED" << std::endl;

						return true;
					}
					break;
					case IDC_RADIO_CARDDRAW_3:
					{
						std::cout << "DIALOG OPTION: CARDDRAW 3 SELECTED" << std::endl;

						return true;
					}
					break;
					default: break;
				}
			}
			else {
			// Couldn't cast game.
			}
		}
	}
	return false;
}